import { AIProviderFactory, ProviderConfigs } from '../services/providers/AIProviderFactory';
import { OpenAIConfig } from '../services/providers/OpenAIProvider';
import { WanxConfig } from '../services/providers/WanxProvider';

// 验证环境变量
const checkEnvVars = () => {
  const openaiVars = ['OPENAI_API_KEY'];
  const wanxVars = ['DASHSCOPE_API_KEY'];
  
  const missingOpenAI = openaiVars.filter(varName => !process.env[varName]);
  const missingWanx = wanxVars.filter(varName => !process.env[varName]);
  
  if (missingOpenAI.length > 0) {
    console.warn(`⚠️ OpenAI配置不完整，缺少环境变量: ${missingOpenAI.join(', ')}`);
  }
  
  if (missingWanx.length > 0) {
    console.warn(`⚠️ 万象配置不完整，缺少环境变量: ${missingWanx.join(', ')}`);
  }
  
  if (missingOpenAI.length > 0 && missingWanx.length > 0) {
    console.warn(`⚠️ 所有AI提供商配置都不完整，AI图像生成功能将无法正常工作`);
  }
};

// OpenAI配置
const openaiConfig: OpenAIConfig = {
  apiKey: process.env.OPENAI_API_KEY || '',
  organization: process.env.OPENAI_ORGANIZATION,
  baseURL: process.env.OPENAI_BASE_URL,
  timeout: parseInt(process.env.OPENAI_TIMEOUT || '60000'),
  maxRetries: parseInt(process.env.OPENAI_MAX_RETRIES || '3'),
  model: process.env.OPENAI_MODEL || 'dall-e-3',
  defaultSize: process.env.OPENAI_DEFAULT_SIZE || '1024x1024',
  defaultQuality: process.env.OPENAI_DEFAULT_QUALITY || 'standard',
  defaultStyle: process.env.OPENAI_DEFAULT_STYLE || 'vivid'
};

// 万象配置
const wanxConfig: WanxConfig = {
  apiKey: process.env.DASHSCOPE_API_KEY || '',
  baseURL: process.env.DASHSCOPE_BASE_URL,
  timeout: parseInt(process.env.DASHSCOPE_TIMEOUT || '30000'),
  maxRetries: parseInt(process.env.DASHSCOPE_MAX_RETRIES || '3'),
  model: process.env.WANX_MODEL || 'wanx-v1',
  endpoint: process.env.WANX_ENDPOINT,
  pollInterval: parseInt(process.env.WANX_POLL_INTERVAL || '2000'),
  maxPollAttempts: parseInt(process.env.WANX_MAX_POLL_ATTEMPTS || '60')
};

// 提示词模板
export const promptTemplates = {
  roomDesign: {
    'living-room': 'A beautiful modern living room with {style} design style, featuring comfortable seating, elegant lighting, and tasteful decor. High quality interior design photography.',
    living: 'A beautiful modern living room with {style} design style, featuring comfortable seating, elegant lighting, and tasteful decor. High quality interior design photography.',
    bedroom: 'A serene and stylish bedroom with {style} design aesthetic, including a comfortable bed, ambient lighting, and harmonious color scheme. Professional interior photography.',
    kitchen: 'A functional and beautiful kitchen with {style} design elements, modern appliances, elegant countertops, and thoughtful storage solutions. High-end interior design photography.',
    bathroom: 'A luxurious and spa-like bathroom with {style} design features, premium fixtures, elegant tiles, and sophisticated lighting. Professional interior photography.',
    office: 'A productive and inspiring home office with {style} design style, ergonomic furniture, organized workspace, and motivating decor. Modern interior design photography.',
    'dining-room': 'An elegant dining room with {style} design aesthetic, beautiful dining table, comfortable seating, and atmospheric lighting. High-quality interior photography.',
    dining: 'An elegant dining room with {style} design aesthetic, beautiful dining table, comfortable seating, and atmospheric lighting. High-quality interior photography.',
    'game-room': 'A fun and entertaining game room with {style} design elements, comfortable gaming furniture, ambient lighting, and recreational equipment. Modern entertainment space photography.',
    basement: 'A transformed basement space with {style} design aesthetic, comfortable furnishing, proper lighting, and functional layout. Professional basement renovation photography.',
    'outdoor-terrace': 'A beautiful outdoor terrace with {style} design style, comfortable outdoor furniture, ambient lighting, and harmonious landscaping. High-quality outdoor living photography.'
  },
  styles: {
    modern: 'clean lines, minimalist furniture, neutral colors, and contemporary materials',
    traditional: 'classic furniture, warm colors, rich textures, and timeless elegance',
    minimalist: 'simple forms, monochromatic palette, uncluttered spaces, and essential furniture',
    scandinavian: 'light woods, white and neutral tones, cozy textiles, and functional design',
    industrial: 'exposed brick, metal fixtures, dark colors, and urban aesthetic',
    bohemian: 'eclectic patterns, vibrant colors, natural materials, and artistic elements',
    tribal: 'ethnic patterns, natural materials, earthy colors, and cultural artifacts',
    cyberpunk: 'neon lighting, futuristic elements, dark colors with bright accents, and high-tech aesthetic',
    coastal: 'light blues and whites, natural textures, beach-inspired decor, and airy atmosphere',
    professional: 'sophisticated furniture, neutral tones, organized layout, and business-appropriate aesthetic'
  }
};

// 初始化AI提供商
export const initializeAIProviders = (): void => {
  checkEnvVars();
  
  const configs: Partial<ProviderConfigs> = {};
  
  // 只有在API Key存在时才添加配置
  if (openaiConfig.apiKey) {
    configs.openai = openaiConfig;
  }
  
  if (wanxConfig.apiKey) {
    configs.wanx = wanxConfig;
  }
  
  AIProviderFactory.initialize(configs);
};

// 导出配置
export { openaiConfig, wanxConfig };
export { AIProviderFactory };